///////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2010 Chris Colinsky
// All Rights Reserved
//
// version	0.5
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

package sugarcookie.utils {
	import sugarcookie.utils.StringUtil;
	
	public class Stopwatch {
		private static var _startTime:Date;
		private static var _stopTime:Date;
		
		public static function start():void {
			_startTime = new Date();
		}
		
		public static function stop():String {
			_stopTime = new Date();
			return formatTime(_stopTime.time - _startTime.time);
		}
		
		public static function formatTime(milliseconds:Number):String {
			var time:String = "";
			var s:Number = int(milliseconds/1000);
			var mill:Number = int(milliseconds%1000);
			
			var m:Number;
			var seconds:Number
			if (s>59) {
				m = int(s/60);
				seconds = int(s%60);
			} else {
				time = "00:00:";
				if (String(s).length==1) time += StringUtil.addPadding(s);
				else time += String(s);
				time += ":"+String(mill);
				return time;
			}
			
			var h:Number;
			var minutes:Number;
			if (m>59) {
				h = int(m/60);
				minutes = int(m%60);
			} else {
				time = "00:";
				if (String(m).length==1) time += StringUtil.addPadding(m);
				else time += String(m);
				time += ":";
				if (String(seconds).length==1) time += StringUtil.addPadding(seconds);
				else time += String(seconds);
				time += ":"+String(mill);
				return time;
			}
			
			if (String(h).length==1) time += StringUtil.addPadding(h);
			else time += String(h);
			time += ":";
			if (String(minutes).length==1) time += StringUtil.addPadding(minutes);
			else time += String(minutes);
			time += ":";
			if (String(seconds).length==1) time += StringUtil.addPadding(seconds);
			else time += String(seconds);
			time += ":"+StringUtil.addPadding(mill,4-String(mill).length);
			
			return time;
		}
	}
}